package javaweb.oa.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String servletPath = request.getServletPath();

        HttpSession session = request.getSession(false);
        if(session!=null&&session.getAttribute("username")!=null) {
            chain.doFilter(request,response);
        }else{
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }
    }
}
